package com.metaapp.demo.custom.recyclerview;

import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;

@SuppressWarnings("all")
public class ViewHolder extends RecyclerView.ViewHolder {
    /**
     * 用来缓存已经找到的界面
     */
    private SparseArray mView;
    public View itemView;
    ViewHolder(View itemView) {
        super(itemView);
        this.mView = new SparseArray();
        this.itemView = itemView;
    }

    public final View getView(int viewId) {
        //对已经有的view作缓存
        View view = this.mView != null ? (View)this.mView.get(viewId) : null;
        if (view == null) {
            //使用缓存减少findviewbyid的次数
            view = this.itemView.findViewById(viewId);
            SparseArray sparseArray = this.mView;
            if (this.mView != null) {
                sparseArray.put(viewId, view);
            }
        }
        return view;
    }

    public final ViewGroup getViewGroup(int viewId) {
        //对已经有的viewGroup做缓存
        View view = this.mView != null ? (View)this.mView.get(viewId) : null;
        if (view == null) {
            //使用缓存减少findviewbyid的次数
            view = this.itemView.findViewById(viewId);
            SparseArray sa = this.mView;
            if (this.mView != null) {
                sa.put(viewId, view);
            }
        }

        if (view == null) {
            throw new RuntimeException("null cannot be cast to non-null type T");
        } else {
            return (ViewGroup)view;
        }
    }

    /**
     * 根据id和内容设置文字
     * @param viewId
     * @param text
     * @return
     */
    public final ViewHolder setText(int viewId, CharSequence text) {
        TextView view = (TextView)this.getView(viewId);
        view.setText((CharSequence)(new StringBuilder()).append("").append(text).toString());
        return this;
    }

    /**
     * 根绝id和内容设置hint
     * @param viewId
     * @param text
     * @return
     */
    public final ViewHolder setHintText(int viewId, CharSequence text) {
        TextView view = (TextView)this.getView(viewId);
        view.setHint((CharSequence)(new StringBuilder()).append("").append(text).toString());
        return this;
    }

    /**
     * 设置图片
     * @param viewId
     * @param resId
     * @return
     */
    public final ViewHolder setImageResource(int viewId, int resId) {
        ImageView iv = (ImageView)this.getView(viewId);
        iv.setImageResource(resId);
        return this;
    }

    /**
     * 设置文字颜色
     * @param viewId
     * @param resId
     * @return
     */
    public final ViewHolder setTextColor(int viewId, int resId) {
        TextView iv = (TextView)this.getView(viewId);
        iv.setTextColor(resId);
        return this;
    }

    /**
     * 设置图片
     * @param viewId
     * @param imageLoader
     * @return
     */
    public final ViewHolder setImagePath(int viewId, ViewHolder.HolderImageLoader imageLoader) {
        ImageView iv = (ImageView)this.getView(viewId);
        imageLoader.loadImage(iv, imageLoader.path);
        return this;
    }

    /**
     * 设置显示影藏
     * @param viewId
     * @param visibility
     * @return
     */
    public final ViewHolder setViewVisibility(int viewId, int visibility) {
        this.getView(viewId).setVisibility(visibility);
        return this;
    }

    public final void setOnItemClickListener( View.OnClickListener listener) {
        this.itemView.setOnClickListener(listener);
    }

    public final void setOnItemLongClickListener( View.OnLongClickListener listener) {
        this.itemView.setOnLongClickListener(listener);
    }

    public abstract static class HolderImageLoader{
       String path;
        public HolderImageLoader(String path) {
            super();
            this.path = path;
        }

        public abstract void loadImage(ImageView iv, String str);


    }
}
